package xyz.thoughtset.viewer.common.crud.core.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import xyz.thoughtset.viewer.common.core.entity.IdMeta;
import xyz.thoughtset.viewer.common.crud.core.util.MPWrapperUtil;

import java.util.LinkedHashMap;
import java.util.List;

@SuppressWarnings({"unchecked","SpringJavaInjectionPointsAutowiringInspection"})
public class BaseQueryServiceImpl<M extends BaseMapper<T>, T extends IdMeta> extends ServiceImpl<M, T> implements BaseService<T> {
    @Autowired
    protected ObjectMapper mapper;
    
    @Override
    public Object createData(LinkedHashMap<String, Object> baseMap){
        throw new UnsupportedOperationException("createData method is not supported, use saveData instead.");
    }
    
    @Override
    public Object updateData(LinkedHashMap<String, Object> baseMap){
        throw new UnsupportedOperationException("createData method is not supported, use saveData instead.");
    }

    @Override
    public Object saveData(LinkedHashMap<String, Object> baseMap){
        throw new UnsupportedOperationException("createData method is not supported, use saveData instead.");
    }

    @Override
    public T saveData(T data) {
        throw new UnsupportedOperationException("saveData method is not supported, use saveData instead.");
    }

    @Override
    public void deleteByPId(String pid) {
        throw new UnsupportedOperationException("createData method is not supported, use saveData instead.");
    }

    @Override
    public boolean deleteById(String pkey ){
        throw new UnsupportedOperationException("createData method is not supported, use saveData instead.");
    }

    @Override
    public T selectDetail(String pkey ){
        return getById(pkey);
    }

    @Override
    public T selectByPId(String pid) {
        QueryWrapper<T> query = new QueryWrapper();
        query.eq("pid",pid);
        query.orderByDesc("id");
        return getOne(query);
    }

    @Override
    public List listByPId(String pid) {
        QueryWrapper<T> query = new QueryWrapper();
        query.eq("pid",pid);
        query.orderByDesc("id");
        return list(query);
    }

    @Override
    public Page selectPage(Page page, LinkedHashMap<String, Object> baseMap){
        return page(page, MPWrapperUtil.buildWrapper(convertValue(baseMap)));
    }

    @Override
    public List selectList(LinkedHashMap<String, Object> baseMap) {
        return list(MPWrapperUtil.buildWrapper(convertValue(baseMap)));
    }

    @SneakyThrows
    public T convertValue(LinkedHashMap<String, Object> baseMap) {
        return mapper.convertValue(baseMap,this.getEntityClass());
    }

    protected QueryWrapper pidQueryWrapper(String pid){
        QueryWrapper query = new QueryWrapper();
        query.eq("pid",pid);
        return query;
    }

}
